package com.hkts.management.unattended.weightingInfo.service.serviceimpl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.unattended.weightingInfo.entity.WeightingInfo;
import com.hkts.management.unattended.weightingInfo.mapper.WeightingInfoMapper;
import com.hkts.management.unattended.weightingInfo.service.IWeightingInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-09-17
 */
@Service
@DS("slave")
public class WeightingInfoServiceImpl extends ServiceImpl<WeightingInfoMapper, WeightingInfo> implements IWeightingInfoService {


    @Autowired
    private WeightingInfoMapper weightingInfoMapper;


    @Override
    public IPage<WeightingInfo> selectPage(Page<WeightingInfo> page, WeightingInfo weightingInfo) {
        QueryWrapper<WeightingInfo> query = Wrappers.query();
        QueryWrapper<WeightingInfo> entityWrapper = getEntityWrapper(query, weightingInfo);
        return weightingInfoMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<WeightingInfo> selectList() {
        QueryWrapper<WeightingInfo> query = Wrappers.query();
        return weightingInfoMapper.selectList(query);
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean save(WeightingInfo weightingInfo){
       int i= weightingInfoMapper.insert(weightingInfo);
       return i > 0;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<WeightingInfo> getEntityWrapper(QueryWrapper<WeightingInfo> entityWrapper, WeightingInfo weightingInfo) {
        //条件拼接
        if (StringUtils.isNotBlank(weightingInfo.getWeighingCode())) {
            entityWrapper.like(WeightingInfo.WEIGHING_CODE, weightingInfo.getWeighingCode());
        }
        if (StringUtils.isNotBlank(weightingInfo.getOrderId())) {
            entityWrapper.like(WeightingInfo.ORDER_ID, weightingInfo.getOrderId());
        }
        if (StringUtils.isNotBlank(weightingInfo.getWaybillCode())) {
            entityWrapper.like(WeightingInfo.WAYBILL_CODE, weightingInfo.getWaybillCode());
        }

        return entityWrapper;
    }

}
